from flask_sqlalchemy import SQLAlchemy
import datetime
db = SQLAlchemy()


user_coupon = db.Table(
    'user_coupon',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
    db.Column('coupon_id', db.Integer, db.ForeignKey('coupon.id'), primary_key=True)
)


#用户表
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20), comment='用户名')
    mobile = db.Column(db.String(16), comment='手机号')
    image = db.Column(db.String(100), comment='头像')
    money = db.Column(db.Integer, comment='余额')
    create_time = db.Column(db.DateTime, default=datetime.datetime.now())
    address = db.relationship('Address', backref='user')


#优惠券表
class Coupon(db.Model):
    __tablename__ = 'coupon'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20), comment='优惠券名')
    price = db.Column(db.Integer, comment='优惠价格')
    rule = db.Column(db.String(40), comment='优惠券规则')
    start_time = db.Column(db.DateTime, default=datetime.datetime.now())
    end_time = db.Column(db.DateTime, default=datetime.datetime.now())
    users = db.relationship('User', secondary=user_coupon, backref=db.backref('coupon'))


#图书级别表:
class BookCate(db.Model):
    __tablename__ = 'bookcate'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(30), comment='类别名')
    cid = db.Column(db.Integer, comment='类别外键')
    ebook = db.relationship('Ebook', backref='bookcate')


#图书信息表:
class Ebook(db.Model):
    __tablename__ = 'ebook'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50), comment='图书名')
    writer = db.Column(db.String(50), comment='作者')
    press = db.Column(db.String(50), comment='出版社')
    price = db.Column(db.Integer, comment='图书价格')
    inventory = db.Column(db.Integer, comment='图书库存')
    sv = db.Column(db.Integer, comment='图书销量')
    image = db.Column(db.String(100), comment='图书封面')
    cate_id = db.Column(db.Integer, db.ForeignKey('bookcate.id'))
    shoppingcart = db.relationship('ShoppingCart', backref='ebook')
    history = db.relationship('History', backref='ebook')

#收货地址表:
class Address(db.Model):
    __tablename__ = 'address'
    id = db.Column(db.Integer, primary_key=True,  autoincrement=True)
    name = db.Column(db.String(50), comment='收货人')
    mobile = db.Column(db.String(16), comment='手机号')
    site = db.Column(db.Text, comment='收货地址')
    is_def = db.Column(db.Boolean, default=False, comment='是否默认')
    is_sel = db.Column(db.Boolean, default=True, comment='是否选择')
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    orderform = db.relationship('OrderForm', backref='address')


#购物车表:
class ShoppingCart(db.Model):
    __tablename__ = 'shoppingcart'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    is_sel = db.Column(db.Boolean, default=False, comment='是否选择')
    create_time = db.Column(db.DateTime, default=datetime.datetime.now(), comment='创建时间')
    ebook_id = db.Column(db.Integer, db.ForeignKey('ebook.id'))
    orderform = db.relationship('OrderForm', backref='shoppingcart')


#订单表:
class OrderForm(db.Model):
    __tablename__ = 'orderform'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now(), comment='创建时间')
    num_pri = db.Column(db.Integer, default=datetime.datetime.now(), comment='总额')
    address_id = db.Column(db.Integer, db.ForeignKey('address.id'))
    shoppingcart_id = db.Column(db.Integer, db.ForeignKey('shoppingcart.id'))


#购买历史表:
class History(db.Model):
    __tablename__ = 'history'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    ebook_id = db.Column(db.Integer, db.ForeignKey('ebook.id'))
